package com.idnx.mynewsdemo.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.idnx.mynewsdemo.R;
import com.idnx.mynewsdemo.adapter.MainAdapter;
import com.idnx.mynewsdemo.database.DataBaseBean;
import com.idnx.mynewsdemo.entity.NewsBean;
import com.idnx.mynewsdemo.presenter.MainViewModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;


public class MainActivity extends AppCompatActivity {
    private MainAdapter mainAdapter;
    private RecyclerView recyclerView;
    private DataBaseBean dataBaseBean;
    private String userName;
    private MainViewModel mainViewModel;
    private List<NewsBean.ResultBean.DataBean> dataBeans;
    private ImageView wode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.main_rv);
        wode = findViewById(R.id.wode);
        dataBaseBean = new DataBaseBean(this);
        dataBeans = new ArrayList<>();
        mainAdapter = new MainAdapter(dataBeans);
        recyclerView.setAdapter(mainAdapter);
        Intent intent = getIntent();
        userName = intent.getStringExtra("username");
        mainViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(MainViewModel.class);
        mainViewModel.getDataBeans().observe(this, dataBeans -> {
            if (dataBeans.isEmpty()) {
                Toast.makeText(MainActivity.this, "无数据", Toast.LENGTH_SHORT).show();
            } else {
                mainAdapter.setNewInstance(dataBeans);
                mainAdapter.notifyDataSetChanged();
            }
        });
        mainAdapter.setOnItemClickListener((adapter, view, position) -> {
            NewsBean.ResultBean.DataBean bean = mainAdapter.getItem(position);
            Intent intent1 = new Intent(MainActivity.this, DetailActivity.class);
            intent1.putExtra("url", bean.getUrl());
            startActivity(intent1);
            saveClickRecord(bean);
        });
        mainViewModel.fetchData();

        wode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent();
                intent1.putExtra("username",userName);
                intent1.setClass(MainActivity.this, MyActivity.class);
                startActivity(intent1);
            }
        });

    }
    private void saveClickRecord(NewsBean.ResultBean.DataBean bean) {
        String formattedDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
        dataBaseBean.saveOrUpdateClickRecord(userName, bean.getUniquekey(), formattedDate, bean.getUrl(), bean.getTitle());
    }

}